/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.apache.dict.service.plugins  
 */
package com.apache.dict.service.plugins;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.dict.entity.DataCate;
import com.apache.dict.manager.DataCateManager;
import com.apache.dict.manager.DataItemManager;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.StrUtil;

/**
 * description:  获取数据字典列表
 * @author dyhou 创建时间：2016-6-23  
 */
public class DictListPluginImpl implements PluginConnector {

	private DataItemManager dataItemManager;

	private DataCateManager dataCateManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity result = new ResultEntity();

		String doCode = String.valueOf(param.getParams("doCode"));

		if (Validator.isNotNull(doCode)) {

			if ("dictCateList".equals(doCode)) {
				dictCateList(result, param);
			}

		} else {

			String cateEname = String.valueOf(param.getParams("cateEname"));
			String fatherValue = String.valueOf(param.getParams("fatherValue"));
			if (Validator.isNotNull(cateEname)) {
				String pageSize = String.valueOf(param.getParams("pageSize"));
				boolean isPage = false;
				if (Validator.isNotNull(pageSize)) {
					String pageIndex = String.valueOf(param.getParams("pageIndex"));
					param.setParams("pageIndex", StrUtil.doNull(pageIndex, "1"));
					isPage = true;
				}
				param.setMethodKey("ByService");
				param.setParams("fatherValue", StrUtil.doNull(fatherValue, "0"));
				Object obj = dataItemManager.execute(param);
				result.setMessage("查询成功！");
				result.setResult("true");
				if (!Validator.isEmpty(obj)) {
					Map<String, Object> data = new HashMap<String, Object>();
					if (isPage) {
						Page page = (Page) obj;
						data.put("total", page.getCount());
						data.put("rows", page.getPageObjects());
					} else {
						List list = (List) obj;
						data.put("total", list.size());
						data.put("rows", list);
					}
					JSONObject json = JSONObject.fromObject(data);
					result.setEntity(json);
				}
			} else {
				result.setMessage("参数为空！");
				result.setResult("false");
			}

		}

		return result;
	}

	private void dictCateList(ResultEntity entity, ParamsVo param) {

		DataCate dataCate = new DataCate();
		dataCate = (DataCate) ClassToolsUtil.getInstance().convert2Bean(param.getParams(), dataCate);

		ParamsVo<DataCate> vo = new ParamsVo<DataCate>();
		vo.setParams("cateEname", Validator.getDefaultStr(dataCate.getCateEname(), ""));
		vo.setParams("cateCname", Validator.getDefaultStr(dataCate.getCateCname(), ""));
		vo.setParams("sysEname", Validator.getDefaultStr(dataCate.getSysEname(), ""));
		vo.setMethodKey("ByProperty");

		String rows = Validator.getDefaultStr(String.valueOf(param.getParams("rows")), "0");
		if ("0".equals(rows)) {
			List list = dataCateManager.getList(vo);
			entity.setEntity(list);
		} else {
			String page = Validator.getDefaultStr(String.valueOf(param.getParams("page")), "1");
			vo.setParams("pageIndex", page);
			vo.setParams("pageSize", rows);
			Page pageList = dataCateManager.getPageInfo(vo);
			entity.setEntity(pageList);
		}
		entity.setMessage("查询成功");

	}

	public void setDataItemManager(DataItemManager dataItemManager) {
		this.dataItemManager = dataItemManager;
	}

	public void setDataCateManager(DataCateManager dataCateManager) {
		this.dataCateManager = dataCateManager;
	}

}
